// subPackages3//pages/liveManage/liveList/liveList.js
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")
import Config from '../../../../utils/config2.js'
import cardConfig from '../../../../config.js'
import Serv from './liveListServ';
var sensorsdata_conf = require("../../../../utils/sensorsdata_conf.js")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card_defaultImg:'https://zysj-pro.oss-cn-shenzhen.aliyuncs.com/zysq-miniprogram/images/card/card_defaultImg.png',
    iconPhone:'https://zysj-pro.oss-cn-shenzhen.aliyuncs.com/app/index/phone.png',
    iconMassage:'https://zysj-pro.oss-cn-shenzhen.aliyuncs.com/app/index/message.png',
    iconMore:'https://zysj-pro.oss-cn-shenzhen.aliyuncs.com/app/index/more.png',
    iconLive:'https://zysj-pro.oss-cn-shenzhen.aliyuncs.com/zysq-miniprogram/images/live/icon_live.png',
    iconPlayback:'https://zysj-pro.oss-cn-shenzhen.aliyuncs.com/zysq-miniprogram/images/live/icon_playback.png',
    iconRecommend:'https://zysj-pro.oss-cn-shenzhen.aliyuncs.com/zysq-miniprogram/images/live/icon_recommend.png',
    dataList:[],
    recommendDataList:[],
    dataStatus:false,
    pageNo:1,
    pageSize:11
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      projectId: options.projectId,
      productId: options.productId
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取直播列表
    let param = {
      projectId:this.data.projectId,
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize,
      sort:'numberOfVisito desc'
    }
    this.getLiveList(1,param,'dataList');
    //推荐直播
    this.getLiveList(2, { recommend: true, productId: this.data.productId, sort: 'numberOfVisito desc'},'recommendDataList');
  },
  /**
   * 加载直播列表
   */
  //type 1 直播 2 回放
  async getLiveList(type,data,lable){ //1直播列表 2推荐直播
    let param = {
      ...data,status:1
    }
    let { rows,success,total,pageNo } = await Serv.getLiveBroadcast(param);
     if(success){
     
      if(type==1){ 
        if(rows.length==0&&pageNo==1){
          this.setData({
            dataStatus:true,
            [lable]:[]
          });
          return;
        }
        let hash = {};
        rows = [...this.data.dataList,...rows] 
        rows = rows.reduceRight((item,next)=>{
          hash[next.id]?'':hash[next.id] = true && item.push(next);
          return item
        },[]); 
        let mobileIds = rows.length>0&&rows.map((item)=>item.counselorPhone);
        this.getEmployeeInfo(mobileIds,rows);
        
      }
      this.setData({
        [lable]:rows,
        total
      });
     }

  },
  toCard(e){
    let {eid} =e.currentTarget.dataset
    wx.navigateTo({url:`/pages/mycard/card?eId=${eid}`})
  },
  toMassage(e){
    let {eid} =e.currentTarget.dataset
    wx.navigateTo({
      url: "/subPackages/pages/chat_detail/chat_detail?eId=" + eid
    });
  },
  callphone(e){
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber:phone
    })
  },
  /**
   * 获取顾问的信息
   */
   async getEmployeeInfo(mobileIds,rows){
     let dataList = rows;
     let { msg,model } = await Serv.getEmployeeInfo(mobileIds);
     if(msg=='success'&&model&&dataList.length>0){
         dataList.forEach(ele => {
           let counselor= model[ele.counselorPhone]
          if(counselor){
            ele.headPic = counselor.headPic;
            ele.chatId = counselor.id;
            ele.labels = counselor.labels?JSON.parse(counselor.labels.replace(/\s*/g,"")):[]
            if(ele.labels.length >2){
              ele.labels = ele.labels.slice(0,2)
            }
          }
      });
      this.setData({
        dataList
       });
     }
     
   },
   /**
    * 跳转直播平台
    */
   
   goLivep(e){
     let { url,id } = e.currentTarget.dataset;
     sensorsdata_conf.server_url = `${Config.host}app/dataAnalysis/create`
     sensorsdata_conf.batch_send.send_timeout = 0
     app.globalData.sensors.setPara(sensorsdata_conf);
     app.globalData.sensors.track('LiveBroadcast', {liveId: id});
     if(url){
       url = url.replace(/.html/,'');
       url = decodeURIComponent(decodeURIComponent(url))
     }
    wx.navigateToMiniProgram({
      appId: 'wxffbb41ec9b99a969',
      path: url,
      success(res) {
      }
    })
   },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    sensorsdata_conf.server_url = `${cardConfig.config.pageDomain}applet/visitLog/create`
    sensorsdata_conf.batch_send.send_timeout = 6000
    app.globalData.sensors.setPara(sensorsdata_conf);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    sensorsdata_conf.server_url = `${cardConfig.config.pageDomain}applet/visitLog/create`
    sensorsdata_conf.batch_send.send_timeout = 6000
    app.globalData.sensors.setPara(sensorsdata_conf);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { dataList, pageNo, total } = this.data;
    if (dataList.length < total) {
      pageNo += 1;
      this.data.pageNo = pageNo;
      let param = {
        projectId:this.data.projectId,
        pageNo:this.data.pageNo,
        pageSize:this.data.pageSize,
        sort: 'numberOfVisito desc'
      }
      this.getLiveList(1,param);
    }
   
  }
})